Position.Center = function(element, parent) {
    var w, h, pw, ph;
    var d = Element.getDimensions(element);
    w = d.width;
    h = d.height;
    //    Position.prepare();
    if (!parent) {
        var ws = windowSize();
        pw = ws[0];
        ph = ws[1];
    } else {
        pw = parent.offsetWidth;
        ph = parent.offsetHeight;
    }
    element.style.top = (ph / 2) - (h / 2) /*- Position.deltaY*/ + "px";
    element.style.left = (pw / 2) - (w / 2) /*- Position.deltaX */ + "px";
};
function windowSize() {
    var winW = 0, winH = 0;
    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName == "Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
    }
    return [winW, winH];
}


var JdsmBlock = Class.create({
    initialize:function(id) {
        this.block = new Element('div', {id: id});
        this.id = id;
    },
    getId: function() {
        return this.id;
    },
    setId: function(newId) {
        this.id = newId;
    },
    getContent: function() {
        return this.block.innerHTML;
    },
    setContent:function(content) {
        this.block.innerHTML = content;
    },
    setWidth: function(width) {
        this.block.style.width = width;
    },
    setHeight: function(height) {
        this.block.style.height = height;
    },
    getWidth: function() {
        return this.block.style.width;
    },
    getHeight: function() {
        return this.block.style.height;
    },
    getElement: function() {
        return this.block;
    }
});

var JdsmWindow = Class.create(JdsmBlock, {
    initialize:function($super, id) {
        $super(id);
    },
    showCenter: function() {
        this.getElement().setStyle(
        {
            position:'absolute',
            background: '#ffffff',
            border:'solid 1px #cccccc',
            zindex: 4
        });
        Position.Center(this.getElement());
    }

});

var JdsmHeader = Class.create(JdsmBlock, {
    initialize:function($super, id) {
        $super(id);
        this.title = "";
        this.getElement().setAttribute('class', 'windowHeaderClass');
    },
    getTitle:function() {
        return this.title;
    },
    setTitle: function(title) {
        this.title = title;
    }
});

var JdsmWindowHeader = Class.create(JdsmWindow, {
    initialize:function($super, id) {
        $super(id);
        this.mainContent = new JdsmBlock(id + '_container');
        this.title = new JdsmHeader(id + '_header');
        Insertion.Top(this.getElement(), this.mainContent.getElement());
        Insertion.Top(this.getElement(), this.title.getElement());
    },
    setContent: function(content) {
        this.mainContent.setContent(content);
    },
    getContent: function() {
        return this.mainContent.getContent();
    },
    getContainer: function() {
        return this.mainContent;
    },
    getHeader: function() {
        return this.title;
    },
    getTitle: function() {
        return this.title.getElement.innerHTML;
    },
    setTitle: function(title) {
        this.title.getElement().innerHTML = title;
    }
});

var JdsmCloseWindowHeader = Class.create(JdsmWindowHeader, {
    initialize:function($super, id) {
        $super(id);
        this.id = id;
        this.img = new Element('img', {alt:'', src:'/img/i/delete.png'});
        Element.setStyle(this.img, {float:'right'});
        this.header = this.getHeader();
        this.getHeader().setHeight('16px');
        this.getHeader().setWidth(this.getWidth());
        Insertion.Top(this.getHeader().getElement(), this.img);

        Event.observe(this.img, 'click', function() {
            Element.remove($(id));
        });
        new Draggable(this.getElement(), {handle:(this.header.getElement())});

    },
    setTitle: function(title) {
        var el = $(this.id + '_header');
        el.innerHTML = '';
        //        el.appendChild(this.img);

        Insertion.Top(el, title);
        Insertion.Top(el, this.img);
    },
    getImgCloseButton: function() {
        return this.img;
    }
});

function showPopupCenter() {
    var win = new JdsmCloseWindowHeader('testId');
    Insertion.Top(document.body, win.getElement());
    win.setTitle("test title");
    win.setTitle("test title2");
    win.setContent("content <b>text</b> value");
    win.setWidth("100px");
    win.setHeight("60px");
    win.showCenter();
}

function postComment(form, content) {
    var par = 'comment.content=' + content;
    $(form).request({
       parameters: par,
        onSuccess: function(transport) {
            var responseText = transport.responseText;
            new Insertion.Top($('comments'), responseText);
            Form.reset($(form));
        }
    });
}


